package com.tianma315.admin.produce.controller;


import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.common.base.BaseController;
import com.tianma315.admin.common.utils.ResponseUtil;
import com.tianma315.admin.common.utils.Result;
import com.tianma315.admin.produce.VO.ScBatFeeVO;
import com.tianma315.admin.produce.domain.ScBatFeeDO;
import com.tianma315.admin.produce.domain.ScBatFeeDO;
import com.tianma315.admin.produce.service.ScAdditiveService;
import com.tianma315.admin.produce.service.ScBatFeeService;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.Calendar;
import java.util.List;

/**
 * 
 * <pre>
 * 配料(生产)
 * </pre>
 * <small> 2019-05-30 09:53:17 | Aron</small>
 */
@Controller
@RequestMapping("/produce/scBatFee")
public class ScBatFeeController extends BaseController{
	@Autowired
	private ScBatFeeService scBatFeeService;
	
	@GetMapping()
//	@RequiresPermissions("produce:scBatFee:scBatFee")
	String ScBatFee(){
	    return "produce/scBatFee/scBatFee";
	}

	@GetMapping("/{produceTaskId}")
	String ScBatFee(@PathVariable("produceTaskId") Integer produceTaskId,Model model){
		List<ScBatFeeDO> list = scBatFeeService.getBatFeeDO(produceTaskId);
		Boolean flag = false;
		if (list==null || list.isEmpty()){
			flag = true;
		}
		model.addAttribute("produceTaskId",produceTaskId);
		model.addAttribute("flag",flag);
		return "produce/scBatFee/scBatFee";
	}
	
	@ResponseBody
	@GetMapping("/list")
//	@RequiresPermissions("produce:scBatFee:scBatFee")
	public Result<Page<ScBatFeeVO>> list(Integer pageNumber, Integer pageSize, ScBatFeeDO scBatFeeDTO){
		// 查询列表数据
        Page<ScBatFeeVO> page = scBatFeeService.getScBatFeeVO(pageNumber,pageSize,scBatFeeDTO);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
//	@RequiresPermissions("produce:scBatFee:add")
	String add(){
	    return "produce/scBatFee/add";
	}

	@GetMapping("/add/{produceTaskId}")
	String add(@PathVariable("produceTaskId")Integer produceTaskId,Model model){
		model.addAttribute("produceTaskId",produceTaskId);
		return "produce/scBatFee/add";
	}

	@GetMapping("/edit/{batFeeId}")
//	@RequiresPermissions("produce:scBatFee:edit")
	String edit(@PathVariable("batFeeId") Integer batFeeId,Model model){
		ScBatFeeVO scBatFee = scBatFeeService.selectVOById(batFeeId);
		model.addAttribute("scBatFee", scBatFee);
	    return "produce/scBatFee/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
//	@RequiresPermissions("produce:scBatFee:add")
	public Result<String> save(@RequestBody ScBatFeeVO scBatFee){

		scBatFee.setCreateBy(getUserId());
		scBatFee.setCreateDate(Calendar.getInstance().getTime());
		if(scBatFeeService.addScBatFeeDO(scBatFee)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
//	@RequiresPermissions("produce:scBatFee:edit")
	public Result<String>  update( @RequestBody ScBatFeeVO scBatFeeVO){
		scBatFeeService.updateScBatFeeVOById(scBatFeeVO);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
//	@RequiresPermissions("produce:scBatFee:remove")
	public Result<String>  remove( Integer batFeeId){
		if(scBatFeeService.deleteScBatFeeVO(batFeeId)){
            return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
//	@RequiresPermissions("produce:scBatFee:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] batFeeIds){
		for (int i = 0; i < batFeeIds.length; i++) {
			scBatFeeService.deleteScBatFeeVO(batFeeIds[i]);
		}
		return Result.ok();
	}

	/**
	 * 导出模板
	 */
	@GetMapping( "/exportBatFee/{produceTaskId}")
	public void exportPurchase(@PathVariable("produceTaskId") String produceTaskId,HttpServletResponse response){
		String fileName = "配料&投料导入模板.xls";
		try {
			response = ResponseUtil.getResponse(response,fileName);
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		HSSFWorkbook wb =scBatFeeService.exportBatFee(produceTaskId);
		try(OutputStream os = response.getOutputStream()) {
			wb.write(os);
			os.flush();
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 进入导入excel
	 */
	@GetMapping( "/batFeeImport")
	public String batFeeImport(MultipartFile file){
		return "produce/scBatFee/batFeeImport";
	}

	/**
	 * 导入excel
	 */
	@ResponseBody
	@PostMapping( "/importBatFee")
	public Result batFeePurchase(MultipartFile file){
		if (!file.isEmpty()){
			scBatFeeService.importBatFee(file,getUserId());
		}
		return Result.ok();
	}
}
